package hash;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.TreeMap;

/**
 * 测试红黑树和哈希表在读取10w个单词过程中添加单词和出现频率方面的效率差别
 * @author yuisama
 * @date 2021/12/15 18:05
 **/
public class HashTableTest {
    public static void main(String[] args) {

        System.out.println("Pride and Prejudice");

        ArrayList<String> words = new ArrayList<>();
        if(FileOperation.readFile("pride-and-prejudice.txt", words)) {
            System.out.println("Total words: " + words.size());

            // Test BST
            long startTime = System.nanoTime();
            // 红黑树存储
            TreeMap<String, Integer> bst = new TreeMap<>();
            for (String word : words) {
                if (bst.containsKey(word))
                    bst.put(word, bst.get(word) + 1);
                else
                    bst.put(word, 1);
            }

            for(String word: words)
                bst.containsKey(word);

            long endTime = System.nanoTime();

            double time = (endTime - startTime) / 1000000000.0;
            System.out.println("红黑树存储共耗时: " + time + " s");

            // Test HashTableByLink
            startTime = System.nanoTime();

             HashTableByLinkedList<String, Integer> ht = new HashTableByLinkedList<>();
            for (String word : words) {
                if (ht.contains(word))
                    ht.put(word, ht.get(word) + 1);
                else
                    ht.put(word, 1);
            }

            for(String word: words)
                ht.contains(word);

            endTime = System.nanoTime();

            time = (endTime - startTime) / 1000000000.0;
            System.out.println("自己实现的哈希表共耗时: " + time + " s");

        }
    }
//    public static void main(String[] args) {
//        HashTableByTree<Integer,String> ht = new HashTableByTree<>();
//        ht.add(1,"h");
//        ht.add(2,"e");
//        ht.add(1,"t");
//        ht.add(17,"hehe");
//        System.out.println(ht.remove(2));
//        System.out.println();
//    }
}